package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class ETCActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back;
    private RelativeLayout etc_state,etc_rl2;
    private AlertDialog.Builder builder;
    private int choice=-1;
    private TextView tv_xiaoshi,tv_title,tv_feilv;
    private RelativeLayout rl,rl_gone,rl_heimingdan;
    private MyApp myApp;
    private HttpUtil util;
    private EditText et_feilv;
    private ImageButton ib_set;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                Toast.makeText(myApp,msg.obj.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_etc);
        initView();
    }

    private void initView() {
        et_feilv=findViewById(R.id.etc_et_feilv);
        ib_set=findViewById(R.id.etc_ib_set);
        img_back=findViewById(R.id.etc_img_back);
        img_back.setOnClickListener(this);
        etc_state=findViewById(R.id.etc_state);
        etc_state.setOnClickListener(this);
        tv_xiaoshi=findViewById(R.id.etc_tv_xiaoshi);
        rl=findViewById(R.id.etc_rl);
        rl.setOnClickListener(this);
        rl_gone=findViewById(R.id.etc_rl_gone);
        rl_heimingdan=findViewById(R.id.etc_heimingdan);
        rl_heimingdan.setOnClickListener(this);
        etc_rl2=findViewById(R.id.etc_rl2);
        etc_rl2.setOnClickListener(this);
        tv_title=findViewById(R.id.etc_tv_title_zhuangtai);
        myApp= (MyApp) getApplication();
        tv_feilv=findViewById(R.id.etc_tv_feilv);
        util=new HttpUtil();
        getfeilv_time();
        ib_set.setOnClickListener(this);
    }


    /**
     *
     *   接口设置可以设置根据 Count 和 Time来设置费率   但在json接口文档中未找到获取Time相关费率的请求
     *
     *   getfeilv_time()   getfeilv_Count() 是一样的请求
     *
     */
    private void getfeilv_time() {
        //json接口中未找到和Time相关的费率请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getEtcRate = util.connectServer("{}", myApp, "GetEtcRate");
                    myApp.setEtcRate(getEtcRate.getString("Money"));
                    tv_feilv.setText(myApp.getEtcRate()+"");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void getfeilv_Count(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getEtcRate = util.connectServer("{}", myApp, "GetEtcRate");
                    myApp.setEtcRate(getEtcRate.getString("Money"));
                    tv_feilv.setText(myApp.getEtcRate()+"");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.etc_img_back:
                finish();
                break;
            case R.id.etc_state:
                String trim = tv_xiaoshi.getText().toString().trim();
                showAlert(trim);
                break;
            case R.id.etc_rl:
                if (rl_gone.getVisibility()==View.GONE){
                    rl_gone.setVisibility(View.VISIBLE);
                }else {
                    rl_gone.setVisibility(View.GONE);
                }
                break;
            case R.id.etc_heimingdan:
                showmultisAlert();
                break;
            case R.id.etc_rl2:
                startActivity(new Intent(ETCActivity.this,ETCMessageActivity.class));
                break;
            case R.id.etc_ib_set:
                if (et_feilv.getText().toString().isEmpty()) {
                    Toast.makeText(myApp, "请输入要更改的费率！", Toast.LENGTH_SHORT).show();
                    return;
                }
                final String feilv_s = et_feilv.getText().toString().trim();
                if (Integer.parseInt(feilv_s)>100||Integer.parseInt(feilv_s)<1){
                    Toast.makeText(myApp, "请输入1-100的数！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (tv_xiaoshi.getText().toString().trim().equals("小时")){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                util.connectServer("{\"RateType\":\"Hour\",\"Money\":"+feilv_s.trim()+"}",myApp,"SetEtcRate");
                                getfeilv_time();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                    getfeilv_time();
                } else if ("状态".equals(tv_xiaoshi.getText().toString().trim())) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                util.connectServer("{\"RateType\":\"Count\",\"Money\":"+feilv_s.trim()+"}",myApp,"SetEtcRate");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                    getfeilv_Count();
                }
                break;
        }
    }

    /**
     * 单选对话框
     */
    private void showAlert(String s){
        final CharSequence[] charSequences = {"小时","状态"};
        final String[] strings={"小时","次"};
        int checkedItem=0;
        for (int i = 0; i <charSequences.length; i++) {
            if (charSequences[i].equals(s)){
                checkedItem=i;
            }
        }
        builder=new AlertDialog.Builder(ETCActivity.this);
        builder.setIcon(R.mipmap.buslogo)
                .setTitle("ETC费率设置")
                .setCancelable(false)
                .setSingleChoiceItems(charSequences, checkedItem, new DialogInterface.OnClickListener() {
                    //单选按钮的点击事件
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //which  被选中的item的position索引
                        choice=which;
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    //确定按钮的点击事件
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (choice != -1) {
                            tv_xiaoshi.setText(charSequences[choice].toString()+"");
                            tv_title.setText("/"+strings[choice].trim());
                            getfeilv_time();
                        }
                    }
                });
        builder.create().show();
    }

    /**
     * 多选框
     */
    private String[]items={"一号小车","二号小车","三号小车","四号小车"};
    private boolean[]ischeckeds={false,false,false,false};
    private void showmultisAlert(){
        builder=new AlertDialog.Builder(ETCActivity.this);
        builder.setIcon(R.mipmap.buslogo)
                .setTitle("黑名单设置")
                .setCancelable(false)
        .setMultiChoiceItems(items, ischeckeds, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                //which 操作的item
                ischeckeds[which]=isChecked;
            }
        })
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String s="";
                        for (int i = 0; i < 4; i++) {
                            if (ischeckeds[i]){
//                                s+=items[i]+"   ";
                                int j=i+1;
                                try {
                                    //这个接口不存在
                                    JSONObject setEtcBlackList = util.connectServer("{\"EtcBlackList\":[{\"CarId\":"+j+"}]}", myApp, "SetEtcBlackList");
                                    s=setEtcBlackList.getString("reason");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        Message message = new Message();
                        message.what=0;
                        message.obj=s;
                        handler.sendMessage(message);
                    }
                }).start();

            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
